﻿using Microsoft.Practices.Prism.Regions;
using productModule.ServiceReference1;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace productModule.Views
{
    /// <summary>
    /// Interaction logic for MulMainView.xaml
    /// </summary>
    [Export("MulMainView")]
    public partial class MulMainView : UserControl
    {
  
        ServiceReference1.Container context =
            new ServiceReference1.Container(new Uri("http://172.16.1.12:11245/odata"));

        public MulMainView()
        {
            InitializeComponent();

             if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
             {
             	//여기에 데이터를 로드하고 결과를 CollectionViewSource에 할당하십시오.
                 System.Windows.Data.CollectionViewSource productsViewSource =
                     (System.Windows.Data.CollectionViewSource)this.Resources["productsViewSource"];
                 productsViewSource.Source = context.products;
             }
        }

        private void Insert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.product_nameTextBox.Text != null)
                {


                    

                    if(this.product_quit_dayDatePicker.SelectedDate != null)
                    {
                        product insertProduct =

                         new product
                         {
                             product_changed_number = this.product_changed_numberTextBox.Text,
                             product_description = this.product_descriptionTextBox.Text,
                             product_name = this.product_nameTextBox.Text,
                             product_put_day = this.product_put_dayDatePicker.SelectedDate.Value,


                             product_quit_day = this.product_quit_dayDatePicker.SelectedDate.Value

                         };

                        context.AddToproducts(insertProduct);
                    }
                    else if(this.product_quit_dayDatePicker.SelectedDate == null)
                    {
                        product insertProduct =

                       new product
                       {
                           product_changed_number = this.product_changed_numberTextBox.Text,
                           product_description = this.product_descriptionTextBox.Text,
                           product_name = this.product_nameTextBox.Text,
                           product_put_day = this.product_put_dayDatePicker.SelectedDate.Value,


                           product_quit_day = null

                       };

                        context.AddToproducts(insertProduct);
                    }

                    

                    

                    //context.AddObject("Location_table", insertLocation);
                   

                    //context.BeginSaveChanges(AsynchCallback, insertLocation);
                    context.SaveChanges();

                    DataServiceCollection<product> lstData = new DataServiceCollection<product>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                    {
                        if (evtargs.Error == null)
                        {
                            this.productsDataGrid.ItemsSource = lstData;
                        }
                    };

                    lstData.LoadAsync(context.products);

                    MessageBox.Show(this.product_nameTextBox.Text + "(이)가 입력되었습니다.", "입력완료 메세지");

                    Textinit();

                    //location_tableDataGrid.DataContext = context.Location_table;

                }
                else
                {
                    MessageBox.Show("입력하시오");
                }
            }
            catch (DataServiceRequestException dataServiceRequestException)
            {
                MessageBox.Show("같은 값이 있네요~");
            }
            catch (FormatException formatException)
            {
                MessageBox.Show("값을 입력하시오");
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (this.product_nameTextBox.Text != null)
            {
                var delete = this.product_nameTextBox.Text;

                //MessageBox.Show(delete);
                product selectItem = context.products
                                            .Where(pr => pr.product_name == delete)
                                            .SingleOrDefault<product>();


                if (selectItem != null)
                {
                    context.DeleteObject(selectItem);
                    context.SaveChanges();

                    DataServiceCollection<product> lstData = new DataServiceCollection<product>();
                    lstData.LoadCompleted += (senderobj, evtargs) =>
                    {
                        if (evtargs.Error == null)
                        {
                            this.productsDataGrid.ItemsSource = lstData;
                        }
                    };

                    lstData.LoadAsync(context.products);

                    MessageBox.Show(selectItem.product_name + "(이)가 삭제되었습니다.", "삭제완료 메세지");

                    Textinit();

                }
                else
                {
                    MessageBox.Show("데이타가 없네요. 지울 데이터를 입력하시오");
                }
            }
            else
            {
                MessageBox.Show("지울 공장 data를 입력하시오");
                this.product_nameTextBox.Focus();
            }
        }

        private void Select_Click(object sender, RoutedEventArgs e)
        {
            product pro = this.productsDataGrid.SelectedItem as product;

            this.product_changed_numberTextBox.Text = pro.product_changed_number;
            this.product_descriptionTextBox.Text = pro.product_description;
            this.product_nameTextBox.Text = pro.product_name;
            this.product_put_dayDatePicker.SelectedDate = pro.product_put_day;
            this.product_quit_dayDatePicker.SelectedDate = pro.product_quit_day;

 
        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {
             try
            {
                if (this.product_nameTextBox.Text != null)
                {
                    var update = this.product_nameTextBox.Text;
                        
                  
                    product selectItem = context.products
                                                .Where(p => p.product_name == update)
                                                .SingleOrDefault<product>();

                    if(product_quit_dayDatePicker.SelectedDate != null)
                    {
                        selectItem.product_changed_number = this.product_changed_numberTextBox.Text;
                        selectItem.product_description = this.product_descriptionTextBox.Text;
                        selectItem.product_name = this.product_nameTextBox.Text;
                        selectItem.product_put_day = this.product_put_dayDatePicker.SelectedDate.Value;
                        selectItem.product_quit_day = this.product_quit_dayDatePicker.SelectedDate.Value;
                    }
                    else
                    {
                        selectItem.product_changed_number = this.product_changed_numberTextBox.Text;
                        selectItem.product_description = this.product_descriptionTextBox.Text;
                        selectItem.product_name = this.product_nameTextBox.Text;
                        selectItem.product_put_day = this.product_put_dayDatePicker.SelectedDate.Value;
                        selectItem.product_quit_day = null;
                    }



                    if (selectItem != null)
                    {
                        context.UpdateObject(selectItem);
                        context.SaveChanges();

                        DataServiceCollection<product> lstData = new DataServiceCollection<product>();
                        lstData.LoadCompleted += (senderobj, evtargs) =>
                        {
                            if (evtargs.Error == null)
                            {
                                this.productsDataGrid.ItemsSource = lstData;
                            }
                        };

                        lstData.LoadAsync(context.products);

                        MessageBox.Show(selectItem.product_name + "의 수정이 완료 되었습니다.", "수정완료 메세지");

                        Textinit();

                    }
                }
            }
            catch(DataServiceQueryException prod)
            {
                MessageBox.Show("product_name은 변경이 불가능 합니다.");

                this.product_nameTextBox.Focus();
            }

           
            
        }

        public void Textinit()
        {
            this.product_changed_numberTextBox.Text = "";
            this.product_descriptionTextBox.Text = "";
            this.product_nameTextBox.Text = "";
            this.product_put_dayDatePicker.SelectedDate = null;
            this.product_quit_dayDatePicker.SelectedDate = null;

        }

    }
}
